package com.kmis.common.security;

import cn.hutool.json.JSONUtil;
import com.kmis.web.ApiResult;
import org.springframework.security.core.Authentication;
import org.springframework.security.web.authentication.logout.LogoutSuccessHandler;
import org.springframework.stereotype.Component;

import javax.servlet.ServletException;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * @author moc
 * 自定义退出
 */
@Component
public class JwtLogoutSuccessHandler implements LogoutSuccessHandler {
    @Override
    public void onLogoutSuccess(HttpServletRequest request, HttpServletResponse response, Authentication authentication) throws IOException, ServletException {
        response.setContentType("application/json;charset=UTF-8");
        ServletOutputStream outputStream =
                response.getOutputStream();
        outputStream.write(JSONUtil.toJsonStr(ApiResult.error("退出成功")).getBytes("UTF-8"));
        outputStream.flush();
        outputStream.close();
    }
}
